{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "97a77730",
   "metadata": {},
   "source": [
    "# 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a95d6640",
   "metadata": {},
   "source": [
    "## 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "de1346a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jack': 4098, 'sape': 4139, 'guido': 4127}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel = {'jack': 4098, 'sape': 4139}\n",
    "tel['guido'] = 4127\n",
    "tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e2a750f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4098"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel['jack']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "293d6d46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jack': 4098, 'guido': 4127, 'irv': 4127}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del tel['sape']\n",
    "tel['irv'] = 4127\n",
    "tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d7472ad8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'guido', 'irv']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(tel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3dae20f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['guido', 'irv', 'jack']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(tel) #sorted()默认是按照升序排列，并且是按照ASCII码表的顺序排列的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0ab332fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'guido' in tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4045d026",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'jack' not in tel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fae74b1",
   "metadata": {},
   "source": [
    "## 字典的查询"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "941fb645",
   "metadata": {},
   "source": [
    ">1. in 方法查询  \n",
    ">2. get 方法获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "97530a43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'huanmin', 'stu2002': 'yuyan', 'stu2003': 'yanzu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu2001\":\"huanmin\",\n",
    "    \"stu2002\":\"yuyan\",\n",
    "    \"stu2003\":\"yanzu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "01220968",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key 是否在字典当中 用 in \n",
    "\"stu2001\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7ebe7236",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'huanmin'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get 方法 获取字典中的值（ key 对应的value内容）\n",
    "info.get(\"stu2001\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a53480fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huanmin\n"
     ]
    }
   ],
   "source": [
    "# 综上所述：字典的查询方法一般步骤如下：\n",
    "if \"stu2001\" in info:\n",
    "    print(info.get(\"stu2001\"))\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2da25967",
   "metadata": {},
   "source": [
    "## 字典的删除"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4aa2c179",
   "metadata": {},
   "source": [
    ">1. pop() 指定值删除\n",
    ">2. popitem() 随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bf134497",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2001': 'huanmin', 'stu2002': 'yuyan', 'stu2003': 'yanzu'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1d5b8364",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'huanmin'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop('stu2001')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e4d6128c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2002': 'yuyan', 'stu2003': 'yanzu'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "42a94f3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu2003', 'yanzu')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "716c8d0c",
   "metadata": {},
   "source": [
    "## 字典一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1066be2f",
   "metadata": {},
   "source": [
    ">1. 字典的循环遍历\n",
    ">2. fromkeys()方法\n",
    ">3. setdefault()方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9131d5f9",
   "metadata": {},
   "source": [
    "### 循环遍历字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b4c79789",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'huanmin', 'stu2002': 'yuyan', 'stu2003': 'yanzu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu2001\":\"huanmin\",\n",
    "    \"stu2002\":\"yuyan\",\n",
    "    \"stu2003\":\"yanzu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f4aa187a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2001': 'huanmin', 'stu2002': 'yuyan', 'stu2003': 'yanzu'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acb22789",
   "metadata": {},
   "source": [
    "###### 循环遍历字典\n",
    "###### 循环遍历的最大用途就是：给所有的值使用同样的 操作/方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7a2b4e1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001 huanmin\n",
      "2002 yuyan\n",
      "2003 yanzu\n"
     ]
    }
   ],
   "source": [
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c370fd06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2001', '2002', '2003']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1a00459",
   "metadata": {},
   "source": [
    "### fromkeys()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "83fcc405",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "82e7461b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud2101': 123456, 'stud2102': 123456, 'stud2103': 123456}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化统一值的字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud2101\",\"stud2102\",\"stud2103\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7f5cc7f",
   "metadata": {},
   "source": [
    "### setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "97a39bcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "64ab5d94",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu2004')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d64dbfef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'kuli'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu2005','kuli')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "89897d32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2001': 'huanmin',\n",
       " 'stu2002': 'yuyan',\n",
       " 'stu2003': 'yanzu',\n",
       " 'stu1804': None,\n",
       " 'stu2004': None,\n",
       " 'stu2005': 'kuli'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbcd28ed",
   "metadata": {},
   "source": [
    "## 课后、课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7acd1437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits={}\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d2abb1f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits[\"apples\"]=10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "00bfa9da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"apples\"in fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "31afcd7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"bananas\"in fruits:\n",
    "    fruits[\"bananas\"] +=1\n",
    "else:\n",
    "    fruits[\"bananas\"] =1\n",
    "fruits\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3c745c93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1, 'pears': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"pears\"not in fruits:\n",
    "    fruits[\"pears\"]=0\n",
    "fruits[\"pears\"]+=1\n",
    "fruits\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f67405e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1, 'pears': 2}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits.setdefault(\"pears\",0)\n",
    "fruits[\"pears\"] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8d0c237",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found={}\n",
    "found['a']=0\n",
    "found['e']=0\n",
    "found['i']=0\n",
    "found['o']=0\n",
    "found['u']=0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f038e5d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:huanmin\n",
      "u\n",
      "a\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "word =input('Provide a word to search for vowels:')\n",
    "found = []\n",
    "for k in word:\n",
    "    if k in vowels:\n",
    "        if k not in found:\n",
    "            found.append(k)\n",
    "for k in found:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5412a00f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:huanmin\n",
      "a was found 1 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "word =input('Provide a word to search for vowels:')\n",
    "found = {}#创建一个空字典\n",
    "found['a']=0\n",
    "found['e']=0\n",
    "found['i']=0\n",
    "found['o']=0\n",
    "found['u']=0\n",
    "for i in word:# 遍历 \n",
    "    if i in vowels:\n",
    "        found[i]+= 1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0f335e4f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "expected 'except' or 'finally' block (Temp/ipykernel_25364/3723849129.py, line 10)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\ZHUANG~1\\AppData\\Local\\Temp/ipykernel_25364/3723849129.py\"\u001b[1;36m, line \u001b[1;32m10\u001b[0m\n\u001b[1;33m    print(k,\"was found\",v,\"time(s)\")\u001b[0m\n\u001b[1;37m                                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expected 'except' or 'finally' block\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "try:\n",
    "    for i in word:\n",
    "        if i in vowels:\n",
    "            found[i] += 1\n",
    "    for k,v in sorted(found.items()):\n",
    "        print(k,\"was found\",v,\"time(s)\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4887d939",
   "metadata": {},
   "source": [
    "###### 上面一个代码报错是因为没有初始化字典的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6322518d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:huanmian\n",
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n",
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels =['a','e','i','o','u']\n",
    "word=input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "found['e'] = found['e']+1\n",
    "found['e'] += 1\n",
    "\n",
    "for k in sorted(found):\n",
    "    print(k,'was found',found[k],'time(s).')# “item”方法传回两个循环变量  \n",
    "    # 在“found”字典上调用“item”方法\n",
    "    \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6dd5243c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0b3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
